#include<stack>
#include<iostream>
#include<map>
#include<queue>
#include<cstring>
using namespace std;


struct var_type{
    std::string variable;
    char variable_type;
};
struct return_var_{
    std::string string_;
    char c;
    char s[1000];
    int i;
    float f;

};

std::map<std::string, var_type> yrmem;//map模拟内存空间
char pd_var_type(std::string letter){
    int pd_i=0,pd_f=0,pd_f2=0;
    if(letter.length()==1&&letter[0]>='A'&&letter[0]<='Z')return 'c';
    for(int i=0;i<letter.length();i++)if(!isdigit(letter[i]))pd_i=1;
    if(pd_i==0) return 'i';
    for(int i=0;i<letter.length();i++)
        if(letter[i]=='.'){
            pd_f++;
        }
    if(pd_f==1&&letter[0]!='.'&&letter[letter.length()-1]!='.')return 'f';
    return 's';
}
void _strcpy(char now[],char old[],int l,int r){
    for(int i=l;i<=r;i++){
        now[i-l]=old[i];
    }
    now[r-l+1]='\0';
}

//======================================================================================================
using std::string;  using std::map;
using std::stack;   using std::queue;
using std::cin;

typedef struct node
{
    double m_num;
    char m_op;
    bool m_flag;
}node;

// string g_Str;
stack<node> g_S;
queue<node> g_Q;
map<int, char> g_Op;

void Change(string g_Str){//中缀转后缀
    double num;
    node temp;
    for (string::size_type i = 0; i != g_Str.size();)
    {
        if(g_Str[i] >= '0' && g_Str[i] <= '9'){
            temp.m_flag = true; //标记是数字
            temp.m_num = g_Str[i++] - '0';//记录这个操作数的第一位
            while(i < g_Str.size() && g_Str[i] >= '0'&&  g_Str[i] <= '9'){//连续读入数字并按数位还原 原始数值
                temp.m_num = temp.m_num * 10 + (g_Str[i] - '0');
                i++;
            }
            g_Q.push(temp);//操作数压入后缀表达式队列
        }else{
            temp.m_flag = false;
            if(g_Str[i] == '(') {
                temp.m_op = g_Str[i];
                g_S.push(temp);//压入操作符栈
                i++;
            }else if(g_Str[i] == ')'){//一直弹出直到遇见左括号
                while(!g_S.empty() && (g_S.top().m_op != '(')){
                    g_Q.push(g_S.top());
                    g_S.pop();
                }
                g_S.pop();//把左括号也弹出
                i++;
            }else{
                //栈顶元素比操作符高，就把栈顶元素弹到后缀表达式中
                while(!g_S.empty() && g_Op[g_S.top().m_op] >= g_Op[g_Str[i]]){
                    g_Q.push(g_S.top());
                    g_S.pop();
                }
                temp.m_op = g_Str[i];
                g_S.push(temp);//压入操作符栈
                i++;
            }
        }
    }

    while(!g_S.empty()){//操作符栈中还有操作符弹到后缀表达队列中
        g_Q.push(g_S.top());
        g_S.pop();
    }

//        while(!g_Q.empty()) //打印后缀队列
//    {
//        if(g_Q.front().m_flag == true){
//            printf("%f ",g_Q.front().m_num);
//        }else{
//            printf("%c ", g_Q.front().m_op);
//        }
//        g_Q.pop();
//    }
//    printf("\n");
}

double Cal(string g_Str){ //计算后缀表达式
    node cur, temp; //temp记录 弹出栈后计算得到值的信息
    double num1,num2;//存储从栈中弹出的两个数值
    while(!g_Q.empty()){
        cur  = g_Q.front();
        g_Q.pop();
        if(cur.m_flag == true) {
            g_S.push(cur);
        }else{
            num2 = g_S.top().m_num;//弹出第二个操作数
            g_S.pop();
            num1 = g_S.top().m_num;//弹出第一个操作数
            g_S.pop();
            temp.m_flag = true;//临时记录操作数为数字
            if(cur.m_op == '+') temp.m_num = num1 + num2;
            else if(cur.m_op == '-') temp.m_num = num1 - num2;
            else if(cur.m_op == '*') temp.m_num = num1 * num2;
            else temp.m_num = num1 / num2;
            g_S.push(temp);//把计算得到的结果（该操作数）压入栈
        }
    }
    return g_S.top().m_num;//栈顶元素就是后缀表达式的值
}

string str_to_num(string mes){
    string out,emp;int t=0,r=0;bool ji[1000];
    memset(ji,0,sizeof(ji));
    for(int i=0;i<mes.length();i++){
        if(mes[i]=='+'||mes[i]=='-'||mes[i]=='*'||mes[i]=='/'||mes[i]=='%'||mes[i]=='('||mes[i]==')'||(mes[i]>'0'&&mes[i]<'9')){
            // out+=mes[i];
            ji[i]=1;
        }
    }
    // for(int j=0;j<1000;j++)cout<<ji[j];
    for(int i=0;i<mes.length();i++){
        cout<<out<<endl;
        if(mes[i]=='+'||mes[i]=='-'||mes[i]=='*'||mes[i]=='/'||mes[i]=='%'||mes[i]=='('||mes[i]==')'||(mes[i]>'0'&&mes[i]<'9'))out+=mes[i];
        else{
            r=i;t=0;
            while(!ji[r]&&r<mes.length()){r++;t++;}
            out+=yrmem[mes.substr(i,t)].variable;
            i+=t;i--;
        }
    }
    cout<<endl<<out;
    return out;
}

int pd_(string g_Str){
    g_Op['+'] = g_Op['-'] = 1;
    g_Op['*'] = g_Op['/'] = 2;
    for (string::iterator it = g_Str.end(); it != g_Str.begin(); it--){
        if(*it == ' ') g_Str.erase(it);
    }
    while(!g_S.empty()) g_S.pop();//初始化栈，在下次计算开始时清空栈内容
    Change(g_Str);
    return Cal(g_Str);
}


int pd(char letter[]){
    int l1=3,r1=3,equal=3,l2,r2,kuoh=1;
    char lmess[1000],rmess[1000];
    while(letter[r1]!='='&&letter[r1+1]!='=')r1++;
    l2=r1+3,r2=l2;
    while(letter[r2+1]!=')'&&kuoh!=0){
        if(letter[r2+1]=='(')kuoh++;
        if(letter[r2+1]==')')kuoh--;
        r2++;
    }
    _strcpy(lmess,letter,l1,r1);
    _strcpy(rmess,letter,l2,r2);
        cout<<lmess<<" "<<l2<<" "<<r2<<" "<<rmess<<"gg";
    strcpy( lmess,str_to_num(lmess).c_str());
    strcpy( rmess,str_to_num(rmess).c_str());
        cout<<lmess<<" "<<l2<<" "<<r2<<" "<<rmess<<"gg";
    if(pd_(lmess)==pd_(rmess))return 1;
    else return 0;
    // return 0;
}


int main(){
    yrmem["a"].variable="1";
    yrmem["bb"].variable="10";
    yrmem["cc"].variable="111";
    yrmem["asda"].variable="100";
    char letter[]="yf(1+(2*4)==99) yrsays(\"ok\") yr";
    cout<<endl;
    if(pd(letter)){
                int p=0;char runlet[1000];
                for(int i=0;i<strlen(letter);i++){
                    if(letter[i]==' '){
                        p=i+1;
                        break;
                    }
                }
                _strcpy(runlet,letter,p,strlen(letter)-1);
                cout<<runlet;
                // runletter_cli(runlet);
            }
    // pd(letter);
    // str_to_num("a+(bb+asda)");
    return 0;
}
